/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package service.inventory.impl;

import dao.inventory.ItemDao;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import model.inventory.Item;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import service.inventory.ItemService;
import utility.Constants;
import utility.NameValueList;
import utility.StringUtils;

/**
 *
 * @author jay
 */
@Service("itemService")
@Transactional(readOnly=true)
public class ItemServiceImpl implements ItemService {
    //avoid LazyInitializationException 
    String mainQuery = "select item from Item item left join fetch item.unit unit "+
            "left join fetch item.principal principal left join fetch item.dept dept "+
            "left join fetch item.packaging packaging";
    
    @Autowired private ItemDao itemDao;

    @Transactional(readOnly=false)
    @Override
    public boolean save(Item data) {
        return itemDao.save(data);
    }

    @Transactional(readOnly=false)
    @Override
    public boolean delete(Item data) {
        return itemDao.delete(data);
    }

    @Override
    public Item getItem(int id) {
        String query = mainQuery+" where item.id = "+id;
        return itemDao.getByCustomQuery(query);
    }

    @Override
    public Item getItem(String kode) {
        String query = mainQuery+" where item.plu = "+utility.StringUtils.fmtSQLString(kode);
        return itemDao.getByCustomQuery(query);
    }

    @Override
    public List<Item> getItem(int start, int num, String params, String orderBy) {
        String query = mainQuery+params+orderBy;
        return itemDao.getAll(start, num, query);
       // return itemDao.getAll(start, num, params, orderBy);
    }
    
   @Override
    public List<Item> getItem(int start, int num, Map<NameValueList, Object> params) {
        String query = mainQuery+StringUtils.getFilter(params);
        return itemDao.getAll(start, num, query);
    }
    
    @Override
    public List<Item> getAutOrder(int day) {
        String query=
                "select distinct item from Item as item, ItemHpp hpp, ItemSupplier isp "+
                "where item.id = hpp.id.itemId and hpp.id.supplierId = isp.id.supplierId "+ 
                "and substring(isp.supplier.suprincipal.jadwal,"+day+",1) = '1'";
        return itemDao.getAll(query);
    }

    @Override
    public List<Item> createDivParent(Map<NameValueList, Object> params) {
        String query = "select distinct divOrder from Item item "+StringUtils.getFilter(params)+
                " order by item.divOrder";
        
        List<Item> nodes = new ArrayList<>();
        List<String> divs = itemDao.getDivs(query);
        for (String div:divs) {
            Item node = new Item(0, div, "", div, "", null);
            nodes.add(node);
        }
        return nodes;
    }
    
    @Override
    public List<Item> getItemTree(String divisi, Map<NameValueList, Object> params) {
        
        Map<NameValueList, Object> newParams = params;
        newParams.put(new NameValueList("item.divOrder", Constants.OPERATOR_EQUAL), divisi); 
        
        String query = "select item.id, item.plu, item.nama, item.divOrder, "+
                "(select supplier.nama from ItemSupplier itemSupplier, Supplier supplier " +
                    "where itemSupplier.aktive = true and itemSupplier.id.supplierId = supplier.id "+
                    "and itemSupplier.id.itemId = item.id) as namaSupplier "+
                "from Item item "+StringUtils.getFilter(params)+
               // "from Item item where item.divOrder = "+StringUtils.fmtSQLString(divisi)+StringUtils.getFilter(params)+
                " order by item.nama";
        
        /*
         select item.id, item.plu, item.nama, item.divOrder, 
                (select supplier.nama from ItemSupplier itemSupplier, Supplier supplier 
                    where itemSupplier.aktive = true and itemSupplier.id.supplierId = supplier.id 
                    and itemSupplier.id.itemId = item.id) as namaSupplier 
                from Item item 
         */
        List<Object[]> divs = (List) itemDao.getAll(query);
        
        List<Item> nodes = new ArrayList<>();
        for(Object[] obj: divs) {
           Item item = new Item(
                   (Integer) obj[0], (String) obj[1], (String) obj[2], (String) obj[3], (String) obj[4], null);
           nodes.add(item);
       }
        return nodes;
    }

    @Override
    public Long count(Map<NameValueList, Object> params) {
        String query= "select count(*) from Item item "+StringUtils.getFilter(params);
        return itemDao.count(query);
    }

    @Override
    public Boolean isExist(Map<NameValueList, Object> params) {
        return count(params) > 0 ? true:false;
    }
    
    
}
